package cn.darkhorse.common.util;

import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;

public class RedisUtil {
    public static Long  leftPush(String key,String json) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        return redisTemplate.opsForList().leftPush(key,json);
    }

    public static String leftPop(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        Object o = redisTemplate.opsForList().leftPop(key);
        return o == null? "" : o.toString();
    }
    public static Long  rightPush(String key,String json) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        return redisTemplate.opsForList().rightPush(key,json);
    }

    public static String rightPop(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        Object o = redisTemplate.opsForList().rightPop(key);
        return o == null? "" : o.toString();
    }

    public static String getValue(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        Object o = redisTemplate.opsForValue().get(key);
        return o == null? "" : o.toString();
    }
    /**
     * 根据key获取当前所有数据
     * @param key
     * @return
     */
    public static List<String> getListAll(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        List<String> o = redisTemplate.opsForList().range(key,0,-1);
        return o == null? new ArrayList<>() : o;
    }
    /**
     * 移除值为value的
     *
     * @param key    键
     * @param values 值
     * @return 移除的个数
     */

    public static  long setRemove(String key, Object values) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        try {
            Long count = redisTemplate.opsForList().remove(key,0,values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 根据key获取参数
     * @param key
     * @return
     */
    public static List<String> getRange(String key,Integer start,Integer end) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        List<String> o = redisTemplate.opsForList().range(key, start, end);
        return o == null? new ArrayList<>() : o;
    }
    /**
     * 是否包含key
     * @param key
     * @return
     */
    public static boolean hasKey(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        boolean o = redisTemplate.hasKey(key);
        return o ;
    }

    public static String getObject(String key) {
        RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        Object o = redisTemplate.opsForHash().get(key,"value");
        return o == null? "" : o.toString();
    }
}
